<template>
  <div class="play-bar">
    <div class="tool" v-if="currentSong" @click="$parent.showPlayBar = false">
      <div class="left">
        <img
          :src="
            currentSong.al
              ? currentSong.al.picUrl
              : currentSong.picUrl | completeImageUrl
          "
          :class="{ playing }"
        />
      </div>

      <div class="middle e_1">
        {{ currentSong.name
        }}<!-- 歌手 -->
        <span
          v-for="artist in currentSong.song
            ? currentSong.song.artists
            : currentSong.ar"
          :key="artist.id"
        >
          {{ artist.name }}
        </span>
        -
        <!-- 专辑 -->
        {{
          currentSong.song ? currentSong.song.album.name : currentSong.al.name
        }}
      </div>

      <div class="right">
        <canvas width="30" height="30" ref="canvas"></canvas>

        <div
          class="item"
          v-if="playing"
          @click.stop="updatePlayingState({ bool: false })"
        >
          <img src="../../assets/暂停.png" alt="" />
        </div>
        <div
          class="item"
          v-else
          @click.stop="updatePlayingState({ bool: true })"
        >
          <img src="../../assets/播放.png" alt="" />
        </div>
        <div class="item" @click.stop="$parent.showMyPlayList = true"></div>
      </div>
    </div>

    <div class="tool" v-else @click="handStop">
      <div class="left"></div>

      <div class="middle e_1">酷狗音乐</div>

      <div class="right">
        <canvas width="30" height="30" ref="canvas"></canvas>

        <div class="item">
          <img src="../../assets/播放.png" @click.stop="handClick" alt="" />
        </div>
        <div class="item" @click.stop="$parent.showMyPlayList = true"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "PlayBar",
  components: {},
  computed: {
    ...mapState(["currentSong", "playing", "currentTime", "duration"]),

    // 已播放百分比
    percentProgress() {
      return this.duration === 0 ? 0 : this.currentTime / this.duration;
    },
  },
  watch: {
    // 画已播放百分比
    percentProgress(n) {
      const { canvas } = this.$refs;
      const context = canvas.getContext("2d");
      context.clearRect(0, 0, 30, 30);
      context.lineWidth = 5;

      context.beginPath();
      context.arc(
        15,
        15,
        14,
        (Math.PI / 180) * -90,
        (Math.PI / 180) * (360 * n - 90)
      );
      context.strokeStyle = "skyblue";
      context.lineWidth = 3;
      context.stroke();

      context.beginPath();
      context.arc(
        15,
        15,
        14,
        (Math.PI / 180) * (360 * n - 90),
        (Math.PI / 180) * 270
      );
      context.strokeStyle = "rgba(0, 0, 0, 0.15)";

      context.stroke();
    },
  },
  methods: {
    ...mapMutations(["updatePlayingState"]),

    // 播放队列没有歌曲，又点击了播放按钮
    handClick() {
      this.$toast.fail({
        position: "middle",
        message: "您还没有添加歌曲",
      });
    },

    // 当前没有curentSong，又点击了PlayBar，此时应该拦截进入
    handStop() {
      this.$toast.fail({
        position: "middle",
        message: "您还没有添加歌曲",
      });
    },
  },
};
</script>
<style lang="less" scoped>
.play-bar {
  width: 100%;
  height: 50px;
  background-color: #fff;
  position: fixed;
  bottom: 40px;
  left: 0;
  z-index: 8;
  border-radius: 20px 20px 0 0;
  box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.3);

  .tool {
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left {
      width: 50px;
      height: 100%;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        animation: rotate linear 10s infinite;
        animation-play-state: paused;

        &.playing {
          animation-play-state: running;
        }
      }
    }

    .middle {
      flex: 1;
      padding-left: 10px;
      color: #707070;
      font-size: 12px;

      span {
        &::after {
          content: "/";
        }

        &:last-of-type {
          &::after {
            content: "";
          }
        }
      }
    }

    .right {
      width: 60px;
      height: 100%;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
      position: relative;

      canvas {
        position: absolute;
        top: 10px;
        left: 0;
        z-index: -1;
      }

      .item {
        width: 30px;
        height: 100%;
        text-align: center;
        line-height: 50px;

        img {
          vertical-align: middle;
        }

        &:last-of-type {
          background-image: url("../../assets/播放列表.png");
          background-repeat: no-repeat;
          background-size: contain;
          background-position: center center;
        }
      }
    }
  }
}

.active {
  background-color: #7070707d;
  border-radius: 50%;
  transition: all 2s;
}

.un_active {
  background-color: #e6e6e6;
  border-radius: 50%;
  transition: all 2s;
}
</style>
